package main

import (
	"fmt"
	"github.com/gin-gonic/gin/binding"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"hosptial-record/global"
	"hosptial-record/initialize"
	validator2 "hosptial-record/validator"
)

func main() {
	// 设置环境
	viper.Set("DEBUG", false)
	initialize.InitConfig()

	// 初始化logger
	initialize.InitLogger()
	// 初始化数据库
	initialize.InitMysql()
	// 初始化表单验证国际化
	if err := initialize.InitTrans("zh"); err != nil {
		// 初始化失败
		panic(err.Error())
	}

	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		// 注册验证器
		_ = v.RegisterValidation("mobile", validator2.ValidateMobile)
		// 注册翻译器
		_ = v.RegisterTranslation("mobile", global.Trans, func(ut ut.Translator) error {
			return ut.Add("mobile", "手机号格式不正确!", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("mobile", fe.Field())
			return t
		})
	}

	//// 线上模式随机端口
	//if !initialize.GetEnvInfo("DEBUG") {
	//	if num, err := utils.GetFreePort(); err == nil {
	//		global.ServerConfig.Port = num
	//	}
	//}

	// 连接redis
	initialize.InitRedis()

	// 注册路由
	engine := initialize.InitRouters()
	zap.S().Infof("启动服务器，端口：%d", global.ServerConfig.Port)
	if err := engine.Run(fmt.Sprintf(":%d", global.ServerConfig.Port)); err != nil {
		zap.S().Errorf("启动失败:%s", err.Error())
	}
	fmt.Println("---")
}
